/**number类的工具
	* @class NumberTool
 */
import {flattenDeep} from "lodash-es"

class NumberTool {

	/**分割数字方法
		* @static
		* @params {num|string} value
		* @params {number} range
		* @params {string} separator
		* @return {string}
	 */
  static split (value: string, range = 3, separator = ',') {
    let endStr: Array<string> | string = [ '' ], _value: any, hasEnd = false;
    value = value + ''

    if (value.includes('.')) {
      hasEnd = true
      _value = value.split('.')
      if (_value[ 0 ].length <= range) return value
      endStr = _value[ 1 ]
    }
    if (!_value && value.length <= range) return value

    if (hasEnd) value = _value[ 0 ]

    value = flattenDeep([ value ])[ 0 ]

    let strArr: any[] = []
    value.split('').reverse().forEach((num, index) => {
      if ((index + 1) % range === 0) {
        strArr.push([ num, separator ])
      } else {
        strArr.push([ num ])
      }
    })

    let endArr = hasEnd ? [ flattenDeep(strArr).reverse(), '.', endStr ] : flattenDeep(strArr).reverse()

    let str = flattenDeep(endArr).join('')
    return str.startsWith(',') ? str.substr(1) : str

  }
}

export default NumberTool
